﻿$(function () {
    var TimeNum1YStart,
        i = 0, j = 0,
        TimeLR,
        TimeNum1YPos = 0;
    var btnXPos,btnYPos;
    document.addEventListener("touchstart", touchStart, false);
    document.addEventListener("touchmove", touchMove, false);
    document.addEventListener("touchend", touchEnd, false);

    function touchStart(e) {
        TimeNum1YStart = e.touches[0].clientY;
        btnXPos = parseInt(document.getElementById("btnSelectCity").offsetLeft);

        //屏幕高度 - 选择城市可见高度 = btnYPos
        btnYPos = parseInt(window.screen.availHeight) - parseInt(document.body.offsetHeight);
    }

    function touchMove(e) {
        e.preventDefault();
        var endX = parseInt(e.changedTouches[0].clientX);
        var endY = parseInt(e.changedTouches[0].clientY);
        if (endX < btnXPos && endY > btnYPos) {
            $('.OrderTimeFir').css('top', parseInt(e.changedTouches[0].clientY - TimeNum1YStart) + parseInt(TimeNum1YPos) + 'px');
            i = Math.abs((parseInt($('.OrderTimeFir').css('top')) - 13) / 27).toFixed(0);
            $('.OrderTimeFir li').removeClass('OrderTimeFirFocus');
            $('.OrderTimeFir li').eq(i).addClass('OrderTimeFirFocus');
        }
    }

    var touchHeight = 27, EndI, EndJ;
    function touchEnd(e) {
        var endX = parseInt(e.changedTouches[0].clientX);
        var endY = parseInt(e.changedTouches[0].clientY);
        if(endX < btnXPos && endY > btnYPos){
            EndI = -(parseInt($('.OrderTimeFir').css('top')) - 13) / touchHeight;
            EndI = EndI.toFixed(0);
            $('.OrderTimeFir').css('top', -touchHeight * (EndI - 1) + 'px');
            $('.OrderTimeFir li').removeClass('OrderTimeFirFocus');
            $('.OrderTimeFir li').eq(EndI).addClass('OrderTimeFirFocus');
            if (parseInt($('.OrderTimeFir').css('top')) >= 0) {
                $('.OrderTimeFir').css('top', '0px');
                $('.OrderTimeFir li').removeClass('OrderTimeFirFocus');
                $('.OrderTimeFir li').eq(1).addClass('OrderTimeFirFocus');
            }
            if (parseInt($('.OrderTimeFir').css('top')) <= -(touchHeight * $('.OrderTimeFir li').length - 1) + 13) {
                $('.OrderTimeFir').css('top', '-54px');
                $('.OrderTimeFir li').removeClass('OrderTimeFirFocus');
                $('.OrderTimeFir li').eq(3).addClass('OrderTimeFirFocus');
            }
            TimeNum1YPos = $('.OrderTimeFir').css('top');
        }
        
    }
});